﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WHM3.AdminPage
{
    
    public partial class Admin : System.Web.UI.Page
    {
        LoginDetails details;
        LinkedList<Shift> shifts;
        protected void Page_Load(object sender, EventArgs e)
        {           
            if (Session["msg"] != null)
            {
                this.msgLabel.Text = (string)Session["msg"];
                this.msgLabel.Visible = true;
                Session["msg"] = "";
            }

            details = (LoginDetails)Session["myobject"];

            try
            {
                if (details == null || BL.getEmployeeByLoginDetails(details).isAdmin.Equals("False"))
                {
                    //hack attempt
                    Response.Redirect("~/General/unAuthorized.aspx");
                }
            }
            catch
            {
                //hack attempt
                Response.Redirect("~/General/unAuthorized.aspx");
            }

            try
            {
                BL.init();
                Employee employee = BL.getEmployeeByLoginDetails(details);
                
                HelloArgs.Text = employee.lastName + " " + employee.firstName + " (" + employee.id + ")";

                //experimental logout feature
                Control h = this.Header.FindControl("LoginView1");
                Control h2 = h.FindControl("loginLink");
                ((System.Web.UI.HtmlControls.HtmlAnchor)h2).HRef = "./Account/Login.aspx";
                ((System.Web.UI.HtmlControls.HtmlAnchor)h2).InnerText = "Log Out";
            }
            catch
            {

            }
        }

        protected void ReportsSystem(object sender, EventArgs e)
        {
            Session["manager"] = true;
            Session["myobject"] = this.details;
            Session["managerDetails"]=this.details;
            Response.Redirect("~/ManagerPage/Reports.aspx");
        }
         
        protected void editEmployeeFunction(object sender, EventArgs e)
        {
            deleteEmployeeDiv.Visible = false;
            EmployeeToEdit.Visible = true;
            editEmployee.Visible = false;
            addNewEmployee.Visible = false;

            this.Label2.Text = "";
            this.Label3.Text = "";
            this.employeeToEditTextBox.Text = "";
        }

        protected void addNewEmployeeFunction(object sender, EventArgs e)
        {
            deleteEmployeeDiv.Visible = false;
            EmployeeToEdit.Visible = false;
            editEmployee.Visible = false;
            addNewEmployee.Visible = true;

            msgLabel.Text = "";

            this.ID.Text = "";
            this.firstName.Text = "";
            this.lastName.Text = "";
            this.firstName.Text = "";
            this.ID.Text = "";
            this.Position.Text = "";
            this.Team.Text = "";
            this.Team.Text = "";
            this.isAdmin.Checked = false;
            this.isManager.Checked = false;
        
        }
        //updateEmployee
        protected void edit(object sender, EventArgs e)
        {

            Employee employee = null;

            string employeeStr = this.employeeToEditTextBox.Text.Trim();
            try
            {
                employee = BL.getEmployeeById(employeeStr);
            }
            catch (ArgumentNullException)
            {
                Label3.Text = "connection error";
                return;
            }

            if (employee == null)
            {
                Label3.Text = "failed to fined employee <br/>";
                return;
            }
            else
            {
                EmployeeToEdit.Visible = false;
                editEmployee.Visible = true;

                Label1.Text = employee.id;
                FirstNameTextBox.Text = employee.firstName;
                LastNameTextBox.Text = employee.lastName;
                PositionTextBox.Text = employee.Position;
                PasswordTextBox.Text = employee.Password;
                managerOfTextBox.Text = employee.managerOf;
                TeamTextBox.Text = employee.Team;

                if (employee.isAdmin.Equals("True"))
                {
                    AdminCheckBox.Checked = true;
                }
                if (employee.isManager.Equals("True"))
                {
                    ManagerCheckBox.Checked = true;
                }
                try
                {
                    shifts = BL.getAllShiftsById(employee.id);
                }
                catch (ArgumentNullException)
                {
                    msgLabel.Text = "connection error";
                    return;
                }
            }
        }

        protected void updateEmployee(object sender, EventArgs e)
        {
            Employee employee = new Employee(Label1.Text.Trim(),
                                            FirstNameTextBox.Text.Trim(),
                                            LastNameTextBox.Text.Trim(),
                                            PasswordTextBox.Text.Trim(),
                                            PositionTextBox.Text.Trim(),
                                            ManagerCheckBox.Checked.ToString(),
                                            AdminCheckBox.Checked.ToString(),
                                            managerOfTextBox.Text.Trim(),
                                            TeamTextBox.Text.Trim(),
                                            minHoursTextBox.Text.Trim());

            bool ans;
            try
            {
                ans = BL.updateEmployee(employee);
            }
            catch (ArgumentNullException)
            {
                Label2.Text = "connection error";
                return;
            }

            if (ans)
            {
                Label2.Text = "update success";
            }
            else
            {
                Label2.Text = "update failed";
            }
        }

        protected void addToDB(object sender, EventArgs e)
        {

            if (this.ID.Text == "" || this.firstName.Text == "" || this.lastName.Text == "" || this.firstName.Text == "" ||
                this.ID.Text == "" || this.Position.Text == "" || this.Team.Text == "" || this.Team.Text == "")
            {
                //Session["msg"] = "fill all fialds";
                //Response.Redirect("~/AdminPage/Admin.aspx");
                this.msgLabel.Text = "all fields are required.";
            }
            else
            {
                try
                {
                    Employee emp = new Employee(this.ID.Text, this.firstName.Text, this.lastName.Text, this.ID.Text, this.Position.Text, this.isManager.Checked.ToString(), this.isAdmin.Checked.ToString(), this.managerOf.Text, this.Team.Text, this.minHour.Text);
                    if (BL.addEmployeeToDB(emp))
                    {
                        //added succesfully
                        //Session["msg"] = " Was added succesfully";
                        //Response.Redirect("~/AdminPage/Admin.aspx");
                        this.msgLabel.Text = "added succesfully";
                    }
                    else
                    {
                        //Session["msg"] = "something went wrong...";
                        //Response.Redirect("~/AdminPage/Admin.aspx");
                        this.msgLabel.Text = "something went wrong...";
                    }
                }
                catch
                {
                    //there was a problem connecting to DB
                    //Session["msg"] =  "There was a problem connecting to the DB";
                    //Response.Redirect("~/AdminPage/Admin.aspx");
                    this.msgLabel.Text = "there was a problem connecting to DB";
                }
            }
        }

        protected void deleteEmployeeFunction(object sender, EventArgs e)
        {
            deleteEmployeeDiv.Visible = true;
            EmployeeToEdit.Visible = false;
            editEmployee.Visible = false;
            addNewEmployee.Visible = false;
        }

        protected void delete(object sender, EventArgs e)
        {
            try
            {
                Employee employee = BL.getEmployeeById(deleteEmployeeTextBox.Text);
                if (BL.deleteEmployee(employee))
                {
                    deleteEmployeeMSG.Text = "deleted";
                }
                else
                {
                    this.deleteEmployeeMSG.Text = "something went wrong...";
                }
                
            }
            catch
            {
                this.deleteEmployeeMSG.Text = "something went wrong...";
            }
        }

        protected void makeCoffe(object sender, EventArgs e)
        {
            Response.Redirect("~/General/MakeCoffee.aspx");
        }
    }
}